import java.util.*;

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) {
            return Collections.emptyList();
        }
        List<String> paths = new ArrayList<>();
        findPaths(root, new StringBuilder(), paths);
        return paths;
    }

    void findPaths(TreeNode root, StringBuilder curPath, List<String> paths) {
        appendNode(curPath, root);
        if (root.left == null && root.right == null) {
            paths.add(curPath.toString());
        }
        if (root.left != null) {
            findPaths(root.left, curPath, paths);
        }
        if (root.right != null) {
            findPaths(root.right, curPath, paths);
        }
        popNode(curPath);
    }

    void appendNode(StringBuilder sb, TreeNode node) {
        if (sb.length() > 0) {
            sb.append("->");
        }
        sb.append(node.val);
    }

    void popNode(StringBuilder sb) {
        int start = sb.lastIndexOf("->");
        if (start == -1) {
            start = 0;
        }
        sb.delete(start, sb.length());
    }
}

//迭代
class Solution1 {
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) {
            return Collections.emptyList();
        }
        Deque<TreeNode> nodeStack = new ArrayDeque<>();
        Deque<List<String>> pathStack = new ArrayDeque<>();
        List<String> paths = new ArrayList<>();
        nodeStack.push(root);
        pathStack.push(new ArrayList<>(Collections.singletonList(Integer.toString(root.val))));
        while (!nodeStack.isEmpty()) {
            TreeNode cur = nodeStack.pop();
            List<String> path = pathStack.pop();
            if (cur.right != null) {
                nodeStack.push(cur.right);
                List<String> rightPath = new ArrayList<>(path);
                rightPath.add(Integer.toString(cur.right.val));
                pathStack.push(rightPath);
            }
            if (cur.left != null) {
                nodeStack.push(cur.left);
                List<String> leftPath = new ArrayList<>(path);
                leftPath.add(Integer.toString(cur.left.val));
                pathStack.push(leftPath);
            }
            if (cur.left == null && cur.right == null) {
                paths.add(String.join("->", path));
            }
        }
        return paths;
    }
}

//层序遍历
class Solution2 {
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) {
            return Collections.emptyList();
        }
        Queue<TreeNode> nodeQueue = new ArrayDeque<>();
        Queue<List<String>> pathQueue = new ArrayDeque<>();
        nodeQueue.offer(root);
        pathQueue.offer(new ArrayList<>(Collections.singletonList(Integer.toString(root.val))));
        List<String> paths = new ArrayList<>();
        while (!nodeQueue.isEmpty()) {
            TreeNode curNode = nodeQueue.poll();
            List<String> curPath = pathQueue.poll();
            if (curNode.left == null && curNode.right == null) {
                paths.add(String.join("->", curPath));
            }
            if (curNode.left != null) {
                nodeQueue.offer(curNode.left);
                List<String> leftPath = new ArrayList<>(curPath);
                leftPath.add(Integer.toString(curNode.left.val));
                pathQueue.offer(leftPath);
            }
            if (curNode.right != null) {
                nodeQueue.offer(curNode.right);
                List<String> rightPath = new ArrayList<>(curPath);
                rightPath.add(Integer.toString(curNode.right.val));
                pathQueue.offer(rightPath);
            }
        }
        return paths;
    }
}